import 'package:dependencies_examples/fl_chart/charts.dart';
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';

class ScrollableExp extends StatelessWidget {
  final List<DataModel> data;
  final double maxY;
  final Color greenColor;
  final Color blueColor;

  const ScrollableExp({
    super.key,
    required this.data,
    this.maxY = 10,
    this.greenColor = Colors.teal,
    this.blueColor = Colors.blue,
  });

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 2,
      child: data.length <= 7
          ? _buildBarChart()
          : SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: SizedBox(
                width: data.length * 60.0,
                child: _buildBarChart()
              ),
            ),
    );
  }

  Widget _buildBarChart() {
    return BarChart(
      BarChartData(
        maxY: maxY,
        alignment: BarChartAlignment.spaceAround,
        barTouchData: BarTouchData(enabled: false),
        titlesData: FlTitlesData(show: false),
        borderData: FlBorderData(show: false),
        gridData: FlGridData(show: false),
        barGroups: _generateBarGroups(),
      ),
    );
  }

  List<BarChartGroupData> _generateBarGroups() {
    return data.map((item) {
      return BarChartGroupData(
        x: data.indexOf(item),
        barRods: [
          BarChartRodData(
            toY: item.mileage,
            color: greenColor,
            width: 8,
            borderRadius: BorderRadius.circular(4),
          ),
          BarChartRodData(
            toY: item.duration,
            color: blueColor,
            width: 8,
            borderRadius: BorderRadius.circular(4),
          ),
        ],
      );
    }).toList();
  }
}
